﻿using System.Drawing.Text;
using DimensionsHelper.Common.TableFormatter;

namespace DimensionsHelper.Client.WinUi.Helpers;

public class FontHelper
{
    private const string FluentUriSystemFontRelativePath = "Assets/Fonts/FluentSystemIcons-Regular.ttf";

    private static readonly Dictionary<string, FontFamily> _systemFontFamiliesMap;


    static FontHelper()
    {
        InstalledFontCollection? fonts = new();
        SystemFontFamilies = fonts.Families.Select(f => new FontFamily(f.Name)).ToArray();
        _systemFontFamiliesMap = SystemFontFamilies.ToDictionary(f => f.Source, StringComparer.OrdinalIgnoreCase);

        DefaultFontFamily = TryGetFontFamily(TableFormatterDefaultValues.FontFamily, out FontFamily? family)
            ? family
            : new FontFamily(TableFormatterDefaultValues.FontFamily);
    }


    public static FontFamily FluentUiSystemFont =>
        new(StorageHelper.GetUriOrPathText($"{FluentUriSystemFontRelativePath}#FluentSystemIcons-Regular"));


    public static FontFamily[] SystemFontFamilies
    {
        get;
    }


    public static FontFamily DefaultFontFamily
    {
        get;
    }


    public static double DefaultFontSize => TableFormatterDefaultValues.FontSize;


    public static bool TryGetFontFamily(string fontName, [MaybeNullWhen(false)] out FontFamily family) =>
        _systemFontFamiliesMap.TryGetValue(fontName, out family);


    public static FontFamily GetFontFamily(string fontName) => _systemFontFamiliesMap[fontName];
}